<?php
require '../database/products.php';
require '../database/categories.php';
?>
<?php 
    $pro=new products();
    $cate=new categories();
    $data=$cate->showAll();
?>
<?php
$error_number="";
$error_price="";
if(isset($_POST['ok'])){
 $price=trim($_POST['price']);
    $number=trim($_POST['number']);
    if($_POST['name']==""||$_POST['price']==""
        ||$_POST['infor']==""||$_POST['full']==""|| $_POST['number']==""
            ||$_POST['date']==""||$_POST['type']==0||$_FILES['img']['name']==""){
        echo 'Truong co dau * la truong bat buoc';
    }  else {
        if(!is_numeric($price)){
            $error_price="Phai la so !";
        }else{
            if(!is_numeric($number)){
                $error_number="Phai la so";
            }else{
              if($_FILES['img']['type'] == "image/jpeg"
			|| $_FILES['img']['type'] == "image/png"
			|| $_FILES['img']['type'] == "image/gif"){
			  if($_FILES['img']['size'] > 1048576){
			      echo "File không được lớn hơn 1mb";
			  }else{
			    echo "Upload: " . $_FILES["img"]["name"] . "<br>";
                            echo "Type: " . $_FILES["img"]["type"] . "<br>";
                            echo "Size: " . ($_FILES["img"]["size"] / 1024) . " kB<br>";
                            echo "Stored in: " . $_FILES["img"]["tmp_name"];
                              move_uploaded_file($_FILES['img']["tmp_name"], "../images/products/".$_FILES["img"]["name"]);
			      if($pro->insert(trim($_POST['name'])
                                      ,  trim($_POST['price'])
                                      ,  trim($_POST['infor'])                                     
                                      ,  trim($_POST['full'])
                                      ,  $_FILES["img"]["name"]
                                      ,  trim($_POST['number'])
                                      ,  trim($_POST['date'])
                                      ,  trim($_POST['type'])                              
                                      )){
//                                       header("location:".baseurl."admin/?page=products&act=list");
//                                       exit();
                                       echo 'Thanh cong';
                                      } else  echo 'Khong the them vao co so du lieu';
                                   
                             
			  }
			}else{
			  // không phải file ảnh
			  echo "Kiểu file không hợp lệ";
			}  
            }
        }
    }
}
?>
<script>
  $(function() {
    $( "#datepicker" ).datepicker({
      showOn: "button",
      buttonImage: "images/iconCalendar.gif",
      buttonImageOnly: true,
       "dateFormat":"yy-mm-dd"
    });
  });
  </script>
<form method="post" action="" enctype="multipart/form-data">
    <table>
        <tr>
            <td>Ten:</td>
            <td><input type="text" name="name" size="30"><span style="color: red;">*</span></td>
        </tr>
        <tr>
            <td>Gia</td>
            <td><input type="text" name="price" size="30"><span style="color: red;">* <?php echo $error_price;?></span></td>
        </tr>
        <tr>
            <td>Thong tin:</td>
            <td><input type="text" name="infor" size="30"><span style="color: red;">*</span></td>
        </tr>
        <tr>
            <td>Dac ta ky thuat:</td>
            <td><textarea cols="40" rows="10" name="full"></textarea><span style="color: red;">*</span></td>
        </tr>
        <tr>
            <td>So luong:</td>
            <td><input type="text" name="number" size="30"><span style="color: red;">*<?php echo $error_number;?></span></td>
        </tr>
        <tr>
            <td>Ngay nhap:</td>
            <td><input type="text" id="datepicker" name="date"><span style="color: red;">*</span></td>
        </tr>
        <tr>
            <td>
                Thuoc Loai:
            </td>
            <td> <select name="type"> 
                    <option value="0">--Loai--</option>
                <?php foreach ($data as $value) {?>
                    <option value="<?php echo $value['cate_Id'];?>"><?php echo $value['cate_name']; ?></option>
               <?php  }?>
                    </select><span style="color: red;">*</span>
            </td>
        </tr>
        <tr>
            <td>Images:</td>
            <td><input type="file" name="img" size="30" id="file"/><span style="color: red;">*</span></td>
        </tr>
        
        <tr>
            <td></td>
            <td><input type="submit" name="ok" value="Them"/></td>
        </tr>
    </table>
</form>